package scau.zxck.view.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import scau.zxck.constant.PATH;
import scau.zxck.utils.PathUtil;
import scau.zxck.view.Main;
import scau.zxck.view.stage.AlertStage;

import java.io.IOException;

/**
 * Created by chenyulin on 2016/11/29.
 */
public class MainController {

    @FXML private Pane mainPane;
    @FXML private MenuItem miNewConn;
    @FXML private MenuItem miSetting;
    @FXML private MenuItem miAbout;

    public void initialize() {
        FXMLLoader connTreeLoader = new FXMLLoader(PathUtil.getResource(PATH.FXML_CONN_TREE_PATH));
        try {
            mainPane.getChildren().clear();
            VBox root = connTreeLoader.load();
            Main.connTreeMainPane = root;
            mainPane.getChildren().add(root);
            miNewConn.setGraphic(new ImageView(new Image("image/create.png")));
            miSetting.setGraphic(new ImageView(new Image("image/settings.png")));
            Main.connTreeController = connTreeLoader.getController();
        } catch (IOException e) {
            new AlertStage("Error", "Failed to load fxml").show();
            e.printStackTrace();
        }
    }

    /**
     * 跳转到连接树界面
     */
    public void toConnTree() {
        if (Main.connTreeMainPane == null) {
            FXMLLoader connTreeLoader = new FXMLLoader(PathUtil.getResource(PATH.FXML_CONN_TREE_PATH));
            try {
                mainPane.getChildren().clear();
                VBox root = connTreeLoader.load();
                Main.connTreeMainPane = root;
                mainPane.getChildren().add(root);
            } catch (IOException e) {
                new AlertStage("Error", "Failed to load fxml").show();
                e.printStackTrace();
            }
        } else {
            mainPane.getChildren().clear();
            mainPane.getChildren().add(Main.connTreeMainPane);
        }
    }

    /**
     * 跳转到表名树界面
     */
    public void toTableTree() {
        FXMLLoader tableTreeLoader = new FXMLLoader(PathUtil.getResource(PATH.FXML_TABLE_TREE_PATH));
        try {
            mainPane.getChildren().clear();
            VBox root = tableTreeLoader.load();
            mainPane.getChildren().add(root);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void miNewConnAction() {
        Main.newConnStage = new Stage();
        Parent root = null;
        try {
            root = FXMLLoader.load(PathUtil.getResource(PATH.FXML_CREATE_CONN_PATH));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Main.newConnStage.setTitle("New Connection");
        Main.newConnStage.setScene(new Scene(root, 270, 370));
        Main.newConnStage.getIcons().add((new Image("image/create.png")));
        Main.newConnStage.setResizable(false);
        Main.newConnStage.setAlwaysOnTop(true);
        Main.newConnStage.show();
    }
    @FXML
    public void miAboutAction() {
        Main.aboutStage = new Stage();
        Parent root = null;
        try {
            root = FXMLLoader.load(PathUtil.getResource(PATH.FXML_ABOUT_PATH));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Main.aboutStage.setTitle("About");
        Main.aboutStage.getIcons().add((new Image("image/zxck_logo.png")));
        Main.aboutStage.setScene(new Scene(root, 240, 194));
        Main.aboutStage.setAlwaysOnTop(true);
        Main.aboutStage.setResizable(false);
        Main.aboutStage.show();
    }
    @FXML
    public void miSettingAction() {
        new AlertStage("Settings", "Todo...").show();
        // TODO 设置菜单功能
    }
}
